package whf.framework.web.struts.converters;

import org.apache.commons.beanutils.ConversionException;

/**
 * 基本的枚举到String类型的转换器
 * @author wanghaifeng
 * @create Dec 30, 2006 2:23:33 PM
 * 
 */
public class BaseEnumToStringConverter extends AbstractConverter {
	public BaseEnumToStringConverter(){
		super(true, null);
		
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
	 */
	public Object convert(Class type, Object value) {
		if(value == null || "".equals(value)){
			if(this.useDefault){
				return this.defaultValue;
			} else {
				throw new ConversionException("No value specified");
			}
		}
		if( value.getClass() == type){
			return value;
		}
		try{
			return Enum.valueOf(type, value.toString());
		}catch(Exception e){
			throw new ConversionException("Unknown specified value!");
		}
	}
	
}
